<template>
    <div class="mod-model">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.name" placeholder="模型名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button type="danger" @click="" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading"  @selection-change="selectionChangeHandle" style="width: 100%;">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column align="center" label="ID" prop="id"></el-table-column>
      <el-table-column align="center" label="流程分类" prop="category"></el-table-column>
      <el-table-column align="center" label="模型名称" prop="name"></el-table-column>
      <el-table-column align="center" label="模型标识" prop="key"></el-table-column>
      <el-table-column align="center" label="模型描述" prop="desc"></el-table-column>
      <el-table-column align="center" label="创建时间" prop="createTime"></el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-dropdown :show-timeout="0" placement="bottom">
            <el-button type="primary">
              功能<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="handleView(scope.row)">模型图</el-dropdown-item>
              <el-dropdown-item divided @click.native="handleDeploy(scope.row)">部署</el-dropdown-item>
              <el-dropdown-item divided @click.native="handleDel(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 -->
    <model-add v-if="modelAddVisible" ref="modelAdd" @refreshDataList="getDataList"></model-add>
  </div>
</template>
<script>

import ModelAdd from './model-add'
import { getModel, deleteModel, warningModel } from '../../api/lcgl/model'
export default {
  data() {
    return {
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      modelAddVisible: false,
      dataForm: {
        current: 1,
        size: 10,
        name: ''
      }
    }
  },
  components: {
    ModelAdd
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      getModel(this.dataForm).then(res => {
        if (res && res.code === 0) {
          this.handleRecords(res.data.records)
          this.dataList = res.data.records
          this.totalPage = res.data.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 新增
    addOrUpdateHandle() {
      this.modelAddVisible = true
      this.$nextTick(() => {
        this.$refs.modelAdd.init()
      })
    },
    handleView(row) {
      const name = `模型id为${row.id}的${row.name}流程图`
      var routeData = this.$router.resolve({ path: `/lcgl/detail/${row.id}`, query: { name: name }})
      window.open(routeData.href, '_blank')
    },
    handleDeploy(row) {
      var _this = this
      this.$confirm('是否确认部署ID为"' + row.id + '"的模型?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return warningModel(row)
      }).then(({ data }) => {
        _this.getDataList()
        _this.$message({
          showClose: true,
          message: '部署成功',
          type: 'success'
        })
      }).catch(function(err) {
        console.log(err)
      })
    },
    handleDel(row) {
      var _this = this
      this.$confirm('是否确认删除ID为"' + row.id + '"的模型?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return deleteModel(row)
      }).then(({ data }) => {
        _this.getDataList()
        _this.$message({
          showClose: true,
          message: '删除成功',
          type: 'success'
        })
      }).catch(function(err) {
        console.log(err)
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    /**
     * 处理数据
     */
    handleRecords(records) {
      for (var i = 0; i < records.length; i++) {
        records[i].desc = JSON.parse(records[i].metaInfo).description
      }
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    }
  }
}
</script>

<style lang="scss" scoped>
.el-pagination {
  text-align: center;
}
</style>
